package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.console.facade.enums.DeviceNetStatus;
import com.econ.powercloud.console.facade.enums.DeviceStatus;
import com.econ.powercloud.ems.dal.entity.IotDevice;
import com.econ.powercloud.ems.dal.mapper.IotDeviceMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
public class DeviceDAO extends ServiceImpl<IotDeviceMapper, IotDevice> {

    private static final Logger logger = LoggerFactory.getLogger(DeviceDAO.class);

    public List<IotDevice> selectByDeviceCodes(List<String> deviceCodes) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(IotDevice::getDeviceCode, deviceCodes);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据productKey集合查询设备有哪些
     */
    public List<IotDevice> selectByProductKeys(List<String> productKeys) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.isEmpty(productKeys)){
            lambdaQueryWrapper.isNull(IotDevice::getProductKey);
        }else {
            lambdaQueryWrapper.in(IotDevice::getProductKey, productKeys);
        }
        return this.list(lambdaQueryWrapper);
    }

    public IotDevice selectByDeviceCode(String deviceCode) {
        return this.getById(deviceCode);
    }

    public List<IotDevice> selectByParentDeviceCode(String parentDeviceCode) {
        return this.list(Wrappers.<IotDevice>lambdaQuery().eq(IotDevice::getParentDeviceCode, parentDeviceCode));
    }

    /**
     * 根据productKey获取还未退出的设备
     *
     * @param productKey
     * @return
     */
    public List<IotDevice> selectActiveDeviceByProductKey(String productKey) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotDevice::getStatus, DeviceStatus.ACTIVE.getCode());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(productKey), IotDevice::getProductKey, productKey);
        List<IotDevice> result = this.baseMapper.selectList(lambdaQueryWrapper);
        return result;
    }

    /**
     * 根据productKey获取已激活的设备
     *
     * @param productKey
     * @return
     */
    public List<IotDevice> selectByPKeyAndDeviceNameAndStatus(String productKey, String deviceName, List<Integer> status) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(IotDevice::getStatusForNet, status);
        if (!StringUtils.isEmpty(deviceName)) {
            lambdaQueryWrapper.eq(IotDevice::getDeviceName, deviceName);
        }
        lambdaQueryWrapper.eq(IotDevice::getProductKey, productKey);
        List<IotDevice> result = this.baseMapper.selectList(lambdaQueryWrapper);
        return result;
    }

    /**
     * 根据productKey和deviceName查询设备
     *
     * @param productKey
     * @return
     */
    public IotDevice selectByProductKeyAndDeviceName(String productKey, String deviceName) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotDevice::getDeviceName, deviceName);
        lambdaQueryWrapper.eq(IotDevice::getProductKey, productKey);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 根据productKey修改该产品下所有设备的changeStatus状态
     */
    public int updateChangeStatusByProductKey(String productKey, Integer changeStatus) {
        LambdaUpdateWrapper<IotDevice> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(IotDevice::getProductKey, productKey).set(IotDevice::getChangeStatus, changeStatus);
        int result = this.baseMapper.update(null, lambdaUpdateWrapper);
        return result;
    }

    public Page<IotDevice> searchDeviceNameByName(String productKey, Integer deviceType,  String consumerConpany, String search, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<IotDevice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(productKey), IotDevice::getProductKey, productKey);
        lambdaQueryWrapper.eq(deviceType != null, IotDevice::getDeviceType, deviceType);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(consumerConpany), IotDevice::getConsumerCompany, consumerConpany);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(search), IotDevice::getSimpleName, search);
        lambdaQueryWrapper.orderByDesc(IotDevice::getGmtCreate);
        Page<IotDevice> iotDevicePage = new Page<>(pageNo, pageSize);
        iotDevicePage = this.baseMapper.selectPage(iotDevicePage, lambdaQueryWrapper);
        return iotDevicePage;
    }

}
